<?php
include '401.php';
include 'includes/Constant.php';
include '../includes/common.php';
include '../ss-config.php';
include '../includes/ss-db.php';
include 'includes/functions.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>积分管理</title>
<link rel="stylesheet" href="css/admin.css" type="text/css" />
<script language="javascript" src="js/ajax.js"></script>
<script language="javascript" src="js/Gensubselect.js"></script>
<script type="text/javascript" src="js/formsubmit.js"></script>
<script type="text/javascript">
function HS_DateAdd(interval,number,date){
    number = parseInt(number);
    if (typeof(date)=="string"){var date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])}
    if (typeof(date)=="object"){var date = date}
    switch(interval){
    case "y":return new Date(date.getFullYear()+number,date.getMonth(),date.getDate()); break;
    case "m":return new Date(date.getFullYear(),date.getMonth()+number,checkDate(date.getFullYear(),date.getMonth()+number,date.getDate())); break;
    case "d":return new Date(date.getFullYear(),date.getMonth(),date.getDate()+number); break;
    case "w":return new Date(date.getFullYear(),date.getMonth(),7*number+date.getDate()); break;
    }
    }
    function checkDate(year,month,date){
    var enddate = ["31","28","31","30","31","30","31","31","30","31","30","31"];
    var returnDate = "";
    if (year%4==0){enddate[1]="29"}
    if (date>enddate[month]){returnDate = enddate[month]}else{returnDate = date}
    return returnDate;
    }
    function WeekDay(date){
    var theDate;
    if (typeof(date)=="string"){theDate = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2]);}
    if (typeof(date)=="object"){theDate = date}
    return theDate.getDay();
    }
    function HS_calender(){
    var lis = "";
    var style = "";
    style +="<style type='text/css'>";
    style +=".calender { width:170px; height:auto; font-size:12px; margin-right:14px; background:url(calenderbg.gif) no-repeat right center #fff; border:1px solid #397EAE; padding:1px}";
    style +=".calender ul {list-style-type:none; margin:0; padding:0;}";
    style +=".calender .day { background-color:#EDF5FF; height:20px;}";
    style +=".calender .day li,.calender .date li{ float:left; width:14%; height:20px; line-height:20px; text-align:center}";
    style +=".calender li a { text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}";
    style +=".calender li a:hover { color:#f30; text-decoration:underline}";
    style +=".calender li a.hasArticle {font-weight:bold; color:#f60 !important}";
    style +=".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}";
    style +=".selectThisYear a, .selectThisMonth a{text-decoration:none; margin:0 2px; color:#000; font-weight:bold}";
    style +=".calender .LastMonth, .calender .NextMonth{ text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}";
    style +=".calender .LastMonth { float:left;}";
    style +=".calender .NextMonth { float:right;}";
    style +=".calenderBody {clear:both}";
    style +=".calenderTitle {text-align:center;height:20px; line-height:20px; clear:both}";
    style +=".today { background-color:#ffffaa;border:1px solid #f60; padding:2px}";
    style +=".today a { color:#f30; }";
    style +=".calenderBottom {clear:both; border-top:1px solid #ddd; padding: 3px 0; text-align:left}";
    style +=".calenderBottom a {text-decoration:none; margin:2px !important; font-weight:bold; color:#000}";
    style +=".calenderBottom a.closeCalender{float:right}";
    style +=".closeCalenderBox {float:right; border:1px solid #000; background:#fff; font-size:9px; width:11px; height:11px; line-height:11px; text-align:center;overflow:hidden; font-weight:normal !important}";
    style +="</style>";
    var now;
    if (typeof(arguments[0])=="string"){
    selectDate = arguments[0].split("-");
    var year = selectDate[0];
    var month = parseInt(selectDate[1])-1+"";
    var date = selectDate[2];
    now = new Date(year,month,date);
    }else if (typeof(arguments[0])=="object"){
    now = arguments[0];
    }
    var lastMonthEndDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+now.getMonth()+"-01").getDate();
    var lastMonthDate = WeekDay(now.getFullYear()+"-"+now.getMonth()+"-01");
    var thisMonthLastDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-01");
    var thisMonthEndDate = thisMonthLastDate.getDate();
    var thisMonthEndDay = thisMonthLastDate.getDay();
    var todayObj = new Date();
    today = todayObj.getFullYear()+"-"+todayObj.getMonth()+"-"+todayObj.getDate();
    for (i=0; i<lastMonthDate; i++){  // Last Month's Date
    lis = "<li class='lastMonthDate'>"+lastMonthEndDate+"</li>" + lis;
    lastMonthEndDate--;
    }
    for (i=1; i<=thisMonthEndDate; i++){ // Current Month's Date
    if(today == now.getFullYear()+"-"+now.getMonth()+"-"+i){
    var todayString = now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-"+i;
    lis += "<li><a href=javascript:void(0) class='today' onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
    }else{
    lis += "<li><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
    }
    }
    var j=1;
    for (i=thisMonthEndDay; i<6; i++){  // Next Month's Date
    lis += "<li class='nextMonthDate'>"+j+"</li>";
    j++;
    }
    lis += style;
    var CalenderTitle = "<a href='javascript:void(0)' class='NextMonth' onclick=HS_calender(HS_DateAdd('m',1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Next Month'>&raquo;</a>";
    CalenderTitle += "<a href='javascript:void(0)' class='LastMonth' onclick=HS_calender(HS_DateAdd('m',-1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Previous Month'>&laquo;</a>";
    CalenderTitle += "<span class='selectThisYear'><a href='javascript:void(0)' onclick='CalenderselectYear(this)' title='Click here to select other year' >"+now.getFullYear()+"</a></span>年<span class='selectThisMonth'><a href='javascript:void(0)' onclick='CalenderselectMonth(this)' title='Click here to select other month'>"+(parseInt(now.getMonth())+1).toString()+"</a></span>月";
    if (arguments.length>1){
    arguments[1].parentNode.parentNode.getElementsByTagName("ul")[1].innerHTML = lis;
    arguments[1].parentNode.innerHTML = CalenderTitle;
    }else{
    var CalenderBox = style+"<div class='calender'><div class='calenderTitle'>"+CalenderTitle+"</div><div class='calenderBody'><ul class='day'><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class='date' id='thisMonthDate'>"+lis+"</ul></div><div class='calenderBottom'><a href='javascript:void(0)' class='closeCalender' onclick='closeCalender(this)'>×</a><span><span><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+todayString+"'>今天</a></span></span></div></div>";
    return CalenderBox;
    }
    }
    function _selectThisDay(d){
    var boxObj = d.parentNode.parentNode.parentNode.parentNode.parentNode;
	var time=d.title.split("-");
	for(var i=1;i<time.length;i++){
		if(time[i].length<2){
			time[i]="0"+time[i];
		}
	}
	d.title=time[0]+"-"+time[1]+"-"+time[2];
    boxObj.targetObj.value = d.title;
    boxObj.parentNode.removeChild(boxObj);
    }
    function closeCalender(d){
    var boxObj = d.parentNode.parentNode.parentNode;
    boxObj.parentNode.removeChild(boxObj);
    }
    function CalenderselectYear(obj){
    var opt = "";
    var thisYear = obj.innerHTML;
    for (i=1970; i<=2020; i++){
    if (i==thisYear){
    opt += "<option value="+i+" selected>"+i+"</option>";
    }else{
    opt += "<option value="+i+">"+i+"</option>";
    }
    }
    opt = "<select onblur='selectThisYear(this)' onchange='selectThisYear(this)' style='font-size:11px'>"+opt+"</select>";
    obj.parentNode.innerHTML = opt;
    }
    function selectThisYear(obj){
    HS_calender(obj.value+"-"+obj.parentNode.parentNode.getElementsByTagName("span")[1].getElementsByTagName("a")[0].innerHTML+"-1",obj.parentNode);
    }
    function CalenderselectMonth(obj){
    var opt = "";
    var thisMonth = obj.innerHTML;
    for (i=1; i<=12; i++){
    if (i==thisMonth){
    opt += "<option value="+i+" selected>"+i+"</option>";
    }else{
    opt += "<option value="+i+">"+i+"</option>";
    }
    }
    opt = "<select onblur='selectThisMonth(this)' onchange='selectThisMonth(this)' style='font-size:11px'>"+opt+"</select>";
    obj.parentNode.innerHTML = opt;
    }
    function selectThisMonth(obj){
    HS_calender(obj.parentNode.parentNode.getElementsByTagName("span")[0].getElementsByTagName("a")[0].innerHTML+"-"+obj.value+"-1",obj.parentNode);
    }
    function HS_setDate(inputObj){
    var calenderObj = document.createElement("span");
    calenderObj.innerHTML = HS_calender(new Date());
    calenderObj.style.position = "absolute";
    calenderObj.targetObj = inputObj;
    inputObj.parentNode.insertBefore(calenderObj,inputObj.nextSibling);
    }
</script>
<script language="JavaScript"> 
//添加与编辑表单共用验证函数
function check_linkform()
{
	//验证积分名称
	if (document.linkform.dona_name.value == "" ) 
	{ 
		alert ("积分人不能为空!"); 
		return false; 
	}
	
	//验证积分文字
	if (document.linkform.dona_money.value == "" ) 
	{ 
		alert ("积分金额不能为空!"); 
		return false; 
	}
}

//数据列表中用于实现全选的函数
function checkall(all)
{
var a = document.getElementsByName("sel_art");
for (var i=0; i<a.length; i++) a[i].checked = all.checked;
}

//用于判断记录是否被选中(用于批量删除)
function del ()
{
　　var flag=true;
　　var temp="";
　　var tmp;
　　if((document.delarticles.sel_art.length+"")=="undefined") {tmp=1}else{tmp=document.delarticles.sel_art.length}
　　if (tmp==1){
　　if (document.delarticles.sel_art.checked){
　　flag=false;
　　temp=document.delarticles.sel_art.value
　　}
　　}else{
　　for (i=0;i<document.delarticles.sel_art.length;i++) {
　　if (document.delarticles.sel_art[i].checked){
　　if (temp==""){
　　flag=false;
　　temp=document.delarticles.sel_art[i].value
　　}else{
　　flag=false;
　　temp = temp +","+ document.delarticles.sel_art[i].value
　　}
　　}
　　}
　　}
　　if (flag){ alert("请选择要删除的数据！")}
　　else{ name=document.delarticles.name.value
　　//alert(name)
　　if (confirm("确实要删除吗？")){
　　window.location="integral_process.php?delitems&item_ids=" + temp;
　　}
　　}
　　return !flag;
}

//删除确认函数
function checkdel()
{
	if (confirm("确实要删除吗？")) {
		return (true);
	}
   else {
		return (false);
	}
}
</script>
</head>
<body>
<div id="wrap">
<h1>
<?php if (isset($_GET['edit'])) {echo '编辑积分'; }else if (isset($_GET['add'])){echo '添加积分';}else {echo '积分管理';}?>
<a href="integral_manage.php?add" target="mainFrame">添加积分</a><a href="integral_manage.php?list" target="mainFrame">管理积分</a>
</h1>
<?php 
//如果是新增积分
if(isset($_GET['add'])) {
?>
<!--新增积分表单开始-->
<form method="post" action="integral_process.php?add" name="linkform" onsubmit="return check_linkform()">
<table border="0" class="formtable">
<tr><td class="label"><label for="vip_number">VIP卡号</label></td><td><input type="text" name="vip_number" id="vip_number" class="textinput" /></td></tr>

<tr><td class="label"><label for="vip_integral">积分数</label></td><td><input type="text" name="vip_integral" id="vip_integral" class="textinput" /></td></tr>

<tr><td class="label"><label for="vip_user_address">用户地址</label></td><td><input type="text" name="vip_user_address" id="vip_user_address" class="textinput" /></td></tr>

<tr><td colspan="2"><input type="submit" name="addnew" value="添加积分" id="submitbutton" class="submit" /></td></tr>
</table>
</form>
<!--新增积分表单结束-->

<?php 
}
 //如果是编辑积分  先获取积分类别id和包含积分各上级类别id的数组
else if (isset($_GET['edit'])) 
{
	$item_id = $_GET['id']; //接收积分id
	$page = $_GET['page']; //接收页码以便在编辑成功后跳转回当前页
	
	$query = sprintf('SELECT ID, VIP_NUMBER, VIP_USER_ADDRESS, VIP_INTEGRAL, SUBMIT_DATE FROM %sintegral WHERE ID = %d', DB_TBL_PREFIX, $item_id);
	
	mysql_query("set names 'utf8'");
	$result = mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
	$row = mysql_fetch_assoc($result);
	
	mysql_free_result($result);
?>
<!--编辑积分表单开始-->
<form method="post" action="integral_process.php?edit" name="linkform" onsubmit="return check_linkform()">
<table border="0" class="formtable">

<tr><td class="label"><label for="vip_number">VIP卡号</label></td><td><input type="text" name="vip_number" id="vip_number" class="textinput" value="<?php echo $row['VIP_NUMBER']; ?>" /></td></tr>

<tr><td class="label"><label for="vip_integral">积分数</label></td><td><input type="text" name="vip_integral" id="vip_integral" class="textinput" value="<?php echo $row['VIP_INTEGRAL']; ?>" /></td></tr>

<tr><td class="label"><label for="vip_user_address">用户地址</label></td><td><input type="text" name="vip_user_address" id="vip_user_address" class="textinput" value="<?php echo $row['VIP_USER_ADDRESS']; ?>" /></td></tr>

<tr><td colspan="2">
<input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
<input type="hidden" name="page" value="<?php echo $page; ?>">
<?php if (isset($_GET['keyword'])) { ?>
<input type="hidden" name="keyword" value="<?php echo $_GET['keyword']; ?>">
<?php } ?>
<input type="submit" value="保存更改" id="submitbutton" class="submit" /></td></tr>
</table>
</form>
<!--编辑积分表单结束-->

<?php
 }
 //如果是显示全部积分列表
 else if (isset($_GET['list'])) 
 {
	$query = sprintf('SELECT ID, VIP_NUMBER, VIP_USER_ADDRESS, VIP_INTEGRAL, SUBMIT_DATE FROM %sintegral', DB_TBL_PREFIX);
	
	
	//如果按积分组筛选 接收参数并定义SQL中的条件语句
	if (isset($_REQUEST['keyword']))
	{
		$keyword = mysql_real_escape_string($_REQUEST['keyword'], $GLOBALS['DB']);
		
		if ($keyword != '')
		{
			$query .= ' WHERE VIP_NUMBER LIKE "%' . $keyword . '%"';
		}
		else
		{
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
			echo '<script>alert("请输入VIP卡号!");history.back(-1);</script>';
		}
	}
			
	$query .= ' ORDER BY SUBMIT_DATE DESC';
	
	//执行第一次查询
	mysql_query("set names 'utf8'");
	$result = mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
	
	 //获取记录总条数
	$num = mysql_num_rows($result); 
	
	$max = 8;  //每页记录数
	$pagenum = ceil($num/$max);  //可分页数
	
	if(!isset($_GET['page']) or !intval($_GET['page']) or !is_numeric($_GET['page']) or $_GET['page'] > $pagenum)
	{
		$page = 1; //当页数不存在 不为十进制数 不是数字 大于可分页数时 为1
	}
	else
	{
		$page = $_GET['page'];  //当前页数
	}
	
	$min = ($page-1)*$max;  //当前页从第$min条记录开始
	
	mysql_free_result($result); //清除第一次查询的结果集
	
	$query .=  " limit $min,$max"; //定义最终的分页SQL语句
	
	// 根据第一次查询的记录数判断  如果有相应数据则执行最终SQL语句并输出积分列表
	if ($num) 
	{
	$dona_result = mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
?>

<!--搜索积分表单开始-->
<form method="post" action="integral_manage.php?list" class="selectform">
<input type="text" name="keyword" size="12">
<input type="submit" value="VIP卡搜索">
</form>
<div style="clear:both;"></div>
<!--搜索表单结束-->


<!--显示积分列表表格-->
<table border="0" cellspacing="0" class="datatable">
<tr><th width="6%">选择</th><th width="25%">VIP卡号</th><th width="15%">积分数</th><th width="30%">用户地址</th><th width="12%">编辑日期</th><th width="6%">编辑</th><th width="6%">删除</th></tr>
<form method="post" action="integral_process.php?delsome" name="delarticles">
<?php
$odd = true;

// 循环输出积分数据行
while($row = mysql_fetch_assoc($dona_result)) 
{
		echo ($odd == true) ? '<tr class="odd">' : '<tr class="even">';
		$odd = !$odd;
		
		//定义编辑与删除积分参数  
		$edit_pars = '&id=' . $row['ID'] . '&page=' . $page;
		
		
		//如果用户执行了搜索 则在链接中传递关键词参数以便在操作完成后返回该关键词搜索结果列表		if (isset($keyword))
		if (isset($keyword))
		{
			$edit_pars .= '&keyword=' . $keyword; 
		}
		
		
		//输出核心数据行
		echo '<td><input type="checkbox" name="sel_art" value="' . $row['ID'] . '"></td><td><a href="?edit' . $edit_pars . '" class="a_man" title="点击编辑">' . $row['VIP_NUMBER'] . '</a></td><td>' . $row['VIP_INTEGRAL'] . '</td><td>' . $row['VIP_USER_ADDRESS'] . '</td><td>' . $row['SUBMIT_DATE'] . '</td><td><a href="?edit' . $edit_pars . '"><img src="images/edit.png"></a></td><td><a href="integral_process.php?del' . $edit_pars . '" onclick="return checkdel()"><img src="images/detip.png" title="删除"></a></td><tr>';		
 }
 	
		mysql_free_result($dona_result);
 ?>
 </table>
 
 <div style="width:150px;float:left;margin:20px 30px 0px 17px;">
 <input type="checkbox" name="chkall" onClick="checkall(this)" class="chkall">全选
 <input type="button" name="btnDelete" value="批量删除" onClick="del()" ID="Button1" class="button">
 </div>
 
 </form>
 
 <div class="web_page">
 <?php
 //定义分页函数所需参数  如果按类别筛选
 $pars = (isset($keyword)) ? '&list&keyword=' . $keyword : '&list';
 
 //调用分页函数
 web_page($pars);
 
 echo '</div>';
  }
 else
	{
		echo '<p class="tip_cates"><img src="images/alert.gif" />暂无相关数据!</p>';
	}
 }
?>
</div>
</body>
</html>
